In a finally block, we need to be able to clean things up from whatever it was that we just did in the corresponding try block. That might involve calling one or more suspend functions. However, if our job was canceled, our cooperative code that we call from finally might elect to not do anything, since the job was canceled. Sometimes, we really do need those suspend calls to do their work, despite the job having been canceled.

To handle this, wrap the suspend calls in a withContext(NonCancellable) block. This will alter our CoroutineContext to ignore any prior cancellation of the job.

You can learn more about this in:
Tags:
Run Edit